namespace ETestIT.Core.Storage
{
    using System.Collections;
    using System.Web;

    public class AppStorage : Store, IAppStorage
    {
        // Fields
        private static Hashtable _internalStorage;

        // Methods

        // Properties
        protected override object LockInstance
        {
            get { return AppStorageLock; }
        }

        protected override bool UseLocking
        {
            get { return true; }
        }

        protected override Hashtable GetInternalHashtable()
        {
            object lockObj;
            if (IsWebApplication)
            {
                var internalHashtable = HttpContext.Current.Application[typeof (AppStorage).FullName] as Hashtable;
                if (internalHashtable == null)
                {
                    lock ((lockObj = AppStorageLock))
                    {
                        internalHashtable = HttpContext.Current.Application[typeof (AppStorage).FullName] as Hashtable;
                        if (internalHashtable == null)
                        {
                            HttpContext.Current.Application[typeof (AppStorage).FullName] =
                                internalHashtable = new Hashtable();
                        }
                    }
                }
                return internalHashtable;
            }
            if (_internalStorage == null)
            {
                lock ((lockObj = SessionStorageLock))
                {
                    if (_internalStorage == null)
                    {
                        _internalStorage = new Hashtable();
                    }
                }
            }
            return _internalStorage;
        }
    }
}